package init

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"monment/basic/config"
	"monment/handler/model"
)

func InitDB() {
	var err error
	mysqlConfig := config.AppCfg.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		mysqlConfig.User, mysqlConfig.Password,
		mysqlConfig.Host, mysqlConfig.Port, mysqlConfig.Database,
	)
	config.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	fmt.Println("数据库连接成功")
	config.DB.AutoMigrate(&model.User{}, &model.Post{}, &model.Like{}, &model.Comment{})
	fmt.Println("迁移成功")
}
